package cyou.breathe.tools;


import org.jetbrains.annotations.NotNull;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: breathe
 * @createTime: 2025-10-31
 */
public class ContentReplaceService {
    private static final String REGEX = "\\[([^]]+)]\\(https?://[\\w-]+(\\.[\\w-]+)+(/[^\\s)]*)?\\)";

    private static final Pattern REPLACE_PATTERN = Pattern.compile(REGEX);

    public static final String REPLACEMENT = "[[%s]]";

    private static final String FILE_PATH = "/Users/breathe/Downloads/breathe-note/breathe-note-EightGuWen";

    private static final String FILE_SUFFIX = ".md";

    public static void main(String[] args) throws IOException {
        Files.walkFileTree(Paths.get(FILE_PATH), new SimpleFileVisitor<>() {

            @Override
            public @NotNull FileVisitResult visitFile(@NotNull Path file, @NotNull BasicFileAttributes attrs) throws IOException {
                if (file.toString().endsWith(FILE_SUFFIX)) {
                    try (FileInputStream fis = new FileInputStream(file.toFile())) {

                        // 读取文件内容
                        StringBuffer content = new StringBuffer(new String(fis.readAllBytes()));

                        // 存储替换后的内容
                        StringBuilder result = new StringBuilder();

                        // 匹配替换内容
                        Matcher matcher = REPLACE_PATTERN.matcher(content);

                        // 发现替换内容
                        while (matcher.find()) {
                            // 替换内容
                            String replacement = String.format(REPLACEMENT, matcher.group(1));
                            matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
                        }

                        // 追加剩余内容
                        matcher.appendTail(result);

                        try (FileOutputStream fos = new FileOutputStream(file.toFile())) {
                            fos.write(result.toString().getBytes());
                            fos.flush();
                        } catch (Exception e) {
                            System.out.println("替换失败");
                        }
                    }
                    System.out.println("替换成功");
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public @NotNull FileVisitResult visitFileFailed(@NotNull Path file, @NotNull IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public @NotNull FileVisitResult postVisitDirectory(@NotNull Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public @NotNull FileVisitResult preVisitDirectory(@NotNull Path dir, @NotNull BasicFileAttributes attrs) throws IOException {
                if ("photo".equals(dir.getFileName().toString()) ||
                        ".obsidian".equals(dir.getFileName().toString()) ||
                        ".DS_Store".equals(dir.getFileName().toString())) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
